<template>
	<view class="player" @tap="ToMusicPlayer()">
		<swiper class="swiper" :current="currentIndex" @change="changeSwiper">
			<swiper-item class="swiper-item" v-for="(item , index) in musicList" :key="index">
				<view class="name">{{item.name}}</view>
				<view class="iconfont play" @tap.stop="exchangeAudioStatus()">
					{{audioStatus?'&#xe64b;':'&#xe620;'}}
				</view>
			</swiper-item>
		</swiper>
		<!-- 		<view class="iconfont more" @tap.stop="test()">
			&#xe831;
		</view> -->
	</view>
</template>

<script>
	export default {
		name: "player",
		data() {
			return {
				currentIndex: 1,
				// 播放器实列状态
				audioStatus: true,
				// 歌曲信息
				musicList: [],
			};
		},
		created() {
			this.currentIndex = 1;
			// 接受手动更新底部播放器的广播
			uni.$on('updatePlayerStatus', () => {
				console.log('手动更新底部播放器>>>>>>>');

				const app = getApp();
				if (app.musicInfo.name) { //正在播放歌曲
					this.musicList = app.musicList;
					this.currentIndex = app.currentIndex;
					this.audioStatus = !app.bgmMusicManager.paused;
					this.$forceUpdate();
				}
			});
			// 一秒后执行，避免实列未初始化
			setTimeout(() => {
				if (this.musicList.length == 0) {
					console.log('自动更新底部播放器>>>>>>>');
					const app = getApp();
					if (!app.musicInfo.name) {
						console.log('无播放源');
						return 0;
					} else {
						this.musicList = app.musicList;
						this.currentIndex = app.currentIndex;
						this.audioStatus = !app.bgmMusicManager.paused;
						this.$forceUpdate();
					}
				}
			}, 1000); // 1000毫秒表示一秒
		},
		mounted() {},
		ready() {},
		methods: {
			test() {
				console.log('无功能');
			},
			changeSwiper(e) {
				const currentIndex = e.detail.current;

				// 确认下一首还是上一首
				let der = null;
				if (currentIndex > this.currentIndex) {
					console.log('>>>');
					der = 1;
				} else if (currentIndex < this.currentIndex) {
					console.log('<<<');
					der = 0;
				}

				this.currentIndex = currentIndex;
				// 播放
				if (der == 0) {
					console.log('上一首');
					uni.$emit('preMusic');
				} else if (der == 1) {
					console.log('下一首');
					uni.$emit('nextMusic');
				}
				this.currentIndex = currentIndex;
				console.log('sss', this.currentIndex);
			},
			/**播放实例状态切换
			 * 
			 */
			exchangeAudioStatus() {
				if (this.audioStatus) {
					uni.$emit('pauseBgm');
					this.audioStatus = false;
				} else {
					uni.$emit('playBgm');
					this.audioStatus = true;
				}
			},
			ToMusicPlayer() {
				console.log('前往音乐播放器');
				uni.navigateTo({
					url: '/pages/musicPlay/musicPlay?musicID=' + this.musicList[this.currentIndex]._id,
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.player {
		background-color: white;
		width: 100%;
		height: 100rpx;
		border-top-right-radius: 30rpx;
		border-top-left-radius: 30rpx;
		box-shadow: 0 0 20rpx 10rpx rgba(0, 0, 0, 0.1);
		display: flex;
		align-items: center;
	}

	.swiper {
		flex: 1;
		height: 100%;
		position: relative;

		.swiper-item {

			.name {
				position: absolute;
				left: 30rpx;
				top: 50%;
				transform: translateY(-50%);
				font-size: $font-size-m-m;
			}

			.iconfont.play {
				position: absolute;
				right: 30rpx;
				top: 50%;
				transform: translateY(-50%);
				font-size: $icon-size-m-m;
			}
		}
	}

	.iconfont.more {
		font-size: $icon-size-m-m;
		margin-right: 30rpx;
	}
</style>